package com.bwie.bwedu.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.bwie.bwedu.R;
import com.bwie.bwedu.bean.Agency;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 沫沫~ on 2016/7/26.
 */
public class DaiLiAdapter extends BaseAdapter implements Filterable {
	private List<String> list;
	private Context context;
	private ViewHolder vh;
	private ArrayList<Agency> mOriginalValues;
	private ArrayFilter mFilter;
	private List<Agency> mObjects;
	private final Object mLock = new Object();

	public DaiLiAdapter(Context context, List<String> list) {
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null){
			convertView = View.inflate(context, R.layout.item_daili,null);
			vh = new ViewHolder();
			vh.tv = (TextView) convertView.findViewById(R.id.daili_tv);
			convertView.setTag(vh);
		}else{
			vh = (ViewHolder) convertView.getTag();
		}
		vh.tv.setText(list.get(position).toString());
		return convertView;
	}

	@Override
	public Filter getFilter() {
		if (mFilter == null) {
			mFilter = new ArrayFilter();
		}
		return mFilter;
	}

	private class ArrayFilter extends Filter {
		@Override
		protected FilterResults performFiltering(CharSequence prefix) {
			FilterResults results = new FilterResults();

			if (mOriginalValues == null) {
				synchronized (mLock) {
					mOriginalValues = new ArrayList<Agency>(mObjects);
				}
			}

			if (prefix == null || prefix.length() == 0) {
				ArrayList<Agency> list;
				synchronized (mLock) {
					list = new ArrayList<Agency>(mOriginalValues);
				}
				results.values = list;
				results.count = list.size();
			} else {
				String prefixString = prefix.toString().toLowerCase();

				ArrayList<Agency> values;
				synchronized (mLock) {
					values = new ArrayList<Agency>(mOriginalValues);
				}

				final int count = values.size();
				final ArrayList<Agency> newValues = new ArrayList<Agency>();

				for (int i = 0; i < count; i++) {
					final Agency value = values.get(i);
					final String valueText = value.toString().toLowerCase();

					// First match against the whole, non-splitted value
					if (valueText.startsWith(prefixString)) {
						newValues.add(value);
					} else {
						final String[] words = valueText.split(" ");
						final int wordCount = words.length;

						// Start at index 0, in case valueText starts with space(s)
						for (int k = 0; k < wordCount; k++) {
							if (words[k].startsWith(prefixString)) {
								newValues.add(value);
								break;
							}
						}
					}
				}
				results.values = newValues;
				results.count = newValues.size();
			}

			return results;
		}

		@Override
		protected void publishResults(CharSequence constraint, FilterResults results) {
			//noinspection unchecked
			mObjects = (List<Agency>) results.values;
			if (results.count > 0) {
				notifyDataSetChanged();
			} else {
				notifyDataSetInvalidated();
			}
		}
	}
	static class ViewHolder{
		TextView tv;
	}
}
